package com.songlanyun.modules.gains.entity;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.math.BigDecimal;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.drive.RootEntity;
import com.songlanyun.common.enums.GoodsConstant;
import com.songlanyun.common.enums.OrderConstant;
import com.songlanyun.common.enums.ShopGainsConstant;
import com.songlanyun.common.utils.SpringContextUtils;
import com.songlanyun.common.utils.custom.MathUtils;
import com.songlanyun.modules.gainsdonation.entity.ShopOrderGainsDonationEntity;
import com.songlanyun.modules.gainsdonation.service.ShopOrderGainsDonationService;
import com.songlanyun.modules.rabbitmq.util.SpringContextUtil;
import com.songlanyun.modules.usergains.entity.UserOrderGains;
import com.songlanyun.modules.usergains.service.UserOrderGainsService;
import lombok.Data;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;

/**
 * 商家销售商品所得表
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2020-12-03 11:03:16
 */
@Data
@TableName("yxt_shop_order_gains")
public class ShopOrderGains extends RootEntity<Long,ShopOrderGains> implements Serializable {
	private static final long serialVersionUID = 1L;

	/**
	 *
	 */
	@TableId
	private Long id;
	/**
	 * 订单id
	 */
	private Long orderId;
	/**
	 * 订单编号
	 */
	private String orderCode;
	/**
	 * 子订单编号
	 */
	private Long childOrderId;
	/**
	 * 0普通商品，1服务商品
	 */
	private ShopGainsConstant.GoodsType goodsType;
	/**
	 * 商品id
	 */
	private Long goodsId;
	/**
	 * 营销活动内类型(0无活动，1秒杀，2拼团）
	 */
	private Integer marketingType;

	/**
	 * 下单渠道
	 */
	private OrderConstant.ChannelType channelType;

	/**
	 * 是否是代销商品
	 */
	private Boolean proxyGoods;

	/**
	 * 礼包类型
	 */
	private GoodsConstant.GiftType giftType;

	/**
	 * 商家id
	 */
	private Long shopId;
	/**
	 * 商家名称
	 */
	private String shopName;
	/**
	 * 商品名称
	 */
	private String goodsTitle;

	/**
	 * 商品短标题
	 */

	private String shortGoodsTitle;
	/**
	 * 商品图片
	 */
	private String goodsImg;

	/**
	 * 实际价格(会员打折或秒杀的实际价格或活动价)
	 */
	private BigDecimal realPrice;

	/**
	 * 原规格销售价
	 */
	private BigDecimal salePrice;
	/**
	 * 总实际价格
	 */
	private BigDecimal totalRealPrice;

	/**
	 * 出货方式（0物流，1自提，2预约）
	 */
	private OrderConstant.ShipmentMode shipmentMode;
	/**
	 * 单位
	 */
	private String unit;

	/**
	 * 商品规格编码
	 */
	private String skuBarCode;
	/**
	 * 规格的Hash编码，方便去对应和查找
	 */
	private String skuHashCode;
	/**
	 * 规格集的文本显示值
	 */
	private String skuSetName;

	//数量
	private Integer number;


	/**
	 * 商品折扣总优惠多少钱
	 */
	private BigDecimal totalReduced;

	/**
	 * 首单优惠分摊多少钱
	 */
	private BigDecimal firstOrderReduced;

	/**
	 * 总促销优惠
	 * @return
	 */
	public BigDecimal getTotalPromotionReduced()
	{
		return MathUtils.add(totalReduced,firstOrderReduced);
	}

	/**
	 * 单位成本
	 */
	private BigDecimal cost;
	/**
	 * 总成本
	 */
	private BigDecimal totalCost;

	/**
	 * 单品标识
	 */
	private Long singleGoodsId;




	/**
	 * 销售所得（扣除了首单优惠，店铺优惠券分摊，店铺优惠券分摊，平台券分摊）
	 */
	private BigDecimal gains;
	/**
	 * 是否存在退款
	 */
	private Boolean existsRefund;
	/**
	 * 退款金额
	 */
	private BigDecimal refundMoney;
	/**
	 * 维权单号
	 */
	private Long rightsId;
	/**
	 * 创建时间
	 */
	@TableField(fill = FieldFill.INSERT)
	private Date createTime;
	/**
	 * 最后一次的计算时间
	 */
	@TableField(fill = FieldFill.INSERT_UPDATE)
	private Date updateTime;
	/**
	 * 结算时间
	 */
	private Date settlementTime;
	/**
	 * 是否已结算
	 */
	private Boolean settlement;
	/**
	 * 会员id
	 */
	private Long userId;

	/**
	 * 商品分区特征(0,普通商品,1激活区商品)
	 */
	private GoodsConstant.AreaFeature areaFeature;


	@Override
	public Long getIdentity() {
		return getId();
	}

	/**
	 * 目前所得只是原所得扣除退款 TODO 做分销时再做分销扣除
	 * @return
	 */

	/**
	 * 实际所得（是扣出退款，扣出分润后的所得）
	 */
	private BigDecimal ableGains;

	/**
	 * 单件需要赠送多少贡献值
	 */
	private BigDecimal consumptionValue= BigDecimal.ZERO ;


	/**
	 * 总共需要赠送多少贡献值
	 */
	private BigDecimal totalConsumptionValue= BigDecimal.ZERO ;

	/**
	 * 商家所得分润之会员分润所有数据
	 */
	@TableField(exist = false)
	private List<UserOrderGains> userOrderGainsList;

	public List<UserOrderGains> loadUserOrderGainsList(Boolean load)
	{
		if(load || userOrderGainsList==null)
		{
			UserOrderGainsService service = (UserOrderGainsService) SpringContextUtil.getBean("userOrderGainsService");
			userOrderGainsList = service.loadByShopGains(getShopId(),getId());
		}
		return userOrderGainsList;
	}

	/**
	 * 分佣总金额度
	 */
	@TableField(exist = false)
	private BigDecimal userOrderGainsSum;

	public BigDecimal loadUserOrderGainsSum()
	{
		List<UserOrderGains> list = this.loadUserOrderGainsList(true);
		BigDecimal sum = BigDecimal.ZERO;
		if(list.size()>0)
		{
			sum = MathUtils.setScale(list.stream().mapToDouble(o->o.getAbleGains().doubleValue()).sum());
		}
		userOrderGainsSum = sum;
		return userOrderGainsSum;
	}

	@TableField(exist = false)
	private BigDecimal shopOrderGainsDonation = BigDecimal.ZERO;

	public BigDecimal loadShopOrderGainsDonation()
	{
		ShopOrderGainsDonationService shopOrderGainsDonationService = SpringContextUtils.getBean("shopOrderGainsDonationService", ShopOrderGainsDonationService.class);
		ShopOrderGainsDonationEntity shopOrderGainsDonationEntity = shopOrderGainsDonationService.loadShopOrderGainsDonation(this.getId());
		if(ObjectUtil.isNotNull(shopOrderGainsDonationEntity))
		{
			this.shopOrderGainsDonation =  shopOrderGainsDonationEntity.getAbleGains();
			return this.shopOrderGainsDonation;
		}
		return this.shopOrderGainsDonation;
	}

	/**
	 * 计算实际所得 之后需要加入分润
	 */
	public  void computeAbleGains()
	{
//		BigDecimal sum = this.loadUserOrderGainsSum();
//		BigDecimal shopPointDonation = this.loadShopOrderGainsDonation();
//		BigDecimal bigDecimal = MathUtils.setScale(this.getGains().doubleValue() - this.getRefundMoney().doubleValue() - sum.doubleValue()-shopPointDonation.doubleValue());
		BigDecimal bigDecimal = MathUtils.setScale(this.getGains().doubleValue() - this.getRefundMoney().doubleValue());
		if(bigDecimal.compareTo(BigDecimal.ZERO)<0)
		{
			bigDecimal= BigDecimal.ZERO;
		}

		this.ableGains = bigDecimal;
	}




}
